package com.yylany.controller;

import com.yylany.emunt.CommonConstants;
import com.yylany.page.PageData;
import com.yylany.page.PageParams;
import com.yylany.pojo.Blog;
import com.yylany.pojo.Comment;
import com.yylany.pojo.House;
import com.yylany.service.BlogService;
import com.yylany.service.CommentService;
import com.yylany.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;


@Controller
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private HouseService houseService;

    @Autowired
    private CommentService commentService;


    @RequestMapping(value = "blog/list", method = {RequestMethod.POST, RequestMethod.GET})
    public String list(Integer pageSize, Integer pageNum, Blog query, ModelMap modelMap) {

        PageData<Blog> ps = blogService.queryBlog(query, PageParams.build(pageSize, pageNum));
        List<House> houses = houseService.getHotHouse(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", houses);
        modelMap.put("ps", ps);

        return "/blog/listing";
    }

    @RequestMapping(value = "blog/detail", method = {RequestMethod.POST, RequestMethod.GET})
    public String blogDetail(int id, ModelMap modelMap) {
        Blog blog = blogService.queryOneBlog(id);
        List<Comment> comments = commentService.getBlogComments(id, 8);
        List<House> houses = houseService.getHotHouse(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", houses);
        modelMap.put("blog", blog);
        modelMap.put("commentList", comments);

        return "/blog/detail";
    }
}
